package com.meilingcloud.biobank.controller;

import cn.hutool.core.collection.ListUtil;
import com.meilingcloud.biobank.domain.Source;
import com.meilingcloud.biobank.domain.dto.*;
import com.meilingcloud.biobank.domain.vo.GetPatientRemindersInfoTodayVo;
import com.meilingcloud.biobank.domain.vo.GetPatientTreatmentInfoVo;
import com.meilingcloud.biobank.domain.vo.GetPatientYYInfoByIdCardVo;
import com.meilingcloud.biobank.domain.vo.GetPatientZDInfoByIdCardVo;
import com.meilingcloud.biobank.service.ISourceService;
import com.meilingcloud.common.annotation.Log;
import com.meilingcloud.common.constant.HttpStatus;
import com.meilingcloud.common.core.controller.BaseController;
import com.meilingcloud.common.core.domain.AjaxResult;
import com.meilingcloud.common.core.page.PageDomain;
import com.meilingcloud.common.core.page.TableDataInfo;
import com.meilingcloud.common.core.page.TableSupport;
import com.meilingcloud.common.enums.BusinessType;
import com.meilingcloud.common.utils.SecurityUtils;
import com.meilingcloud.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * 样本源 Controller
 *
 * @author chenl
 * @date 2021-02-26
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/biobank/source")
public class SourceController extends BaseController {

    private final ISourceService iSourceService;

    /**
     * 查询样本源 列表
     */
    @PreAuthorize("@ss.hasPermi('biobank:source:list')")
    @GetMapping("/list")
    public TableDataInfo list(Source source) {
        startPage();
        List<Source> list = iSourceService.queryList(source);
        iSourceService.queryListWithSourceType(list);
        return getDataTable(list);
    }

    /**
     * 导出样本源 列表
     */
    @PreAuthorize("@ss.hasPermi('biobank:source:export')")
    @Log(title = "样本源 ", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Source source) {
        List<Source> list = iSourceService.queryList(source);
        List<Source> sourceList = iSourceService.queryListWithSourceType(list);
        ExcelUtil<Source> util = new ExcelUtil<Source>(Source.class);
        return util.exportExcel(sourceList, "样本源数据");
    }

    /**
     * 导入样本源
     */
    @PreAuthorize("@ss.hasPermi('biobank:source:import')")
    @Log(title = "样本源 ", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<Source> util = new ExcelUtil<>(Source.class);
        List<Source> sources = util.importExcel(file.getInputStream());
        String message = iSourceService.importSource(sources, updateSupport);
        return AjaxResult.success(message);
    }

    /**
     * 导入样本源 模板
     */
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<Source> util = new ExcelUtil<>(Source.class);
        return util.importTemplateExcel("样本源导入模板");
    }

    /**
     * 获取样本源 详细信息
     */
    @PreAuthorize("@ss.hasPermi('biobank:source:query')")
    @GetMapping(value = "/{sourceId}")
    public AjaxResult getInfo(@PathVariable("sourceId") Long sourceId) {
        return AjaxResult.success(iSourceService.queryById(sourceId));
    }

    /**
     * 新增样本源
     */
    @PreAuthorize("@ss.hasPermi('biobank:source:add')")
    @Log(title = "样本源 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody Source source) {
        source.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iSourceService.saveSource(source));
    }

    /**
     * 修改样本源
     */
    @PreAuthorize("@ss.hasPermi('biobank:source:edit')")
    @Log(title = "样本源 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody Source source) {
        source.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(iSourceService.updateSource(source));
    }

    /**
     * 删除样本源
     */
    @PreAuthorize("@ss.hasPermi('biobank:source:remove')")
    @Log(title = "样本源 ", businessType = BusinessType.DELETE)
    @DeleteMapping("/{sourceIds}")
    public AjaxResult remove(@PathVariable Long[] sourceIds) {
        return toAjax(iSourceService.removeByIds(Arrays.asList(sourceIds)));
    }

    /**
     * 根据code查询样本源
     */
    @PreAuthorize("@ss.hasPermi('biobank:source:query')")
    @GetMapping(value = "getByCode/{sourceCode}")
    public AjaxResult getDetailByCode(@PathVariable("sourceCode") String sourceCode) {
        return AjaxResult.success(iSourceService.queryByCode(sourceCode));
    }

    /**
     * 根据code或名称模糊查询样本源
     */
    @PreAuthorize("@ss.hasPermi('biobank:source:query')")
    @GetMapping(value = "getByCodeOrName/{codeOrName}")
    public AjaxResult getDetailByName(@PathVariable("codeOrName") String codeOrName) {
        return AjaxResult.success(iSourceService.getDetailByCodeOrName(codeOrName));
    }

    /**
     * 根据样本源id查询样本源信息
     */
    @PreAuthorize("@ss.hasPermi('biobank:source:query')")
    @GetMapping(value = "getSourceInfo/{sourceId}")
    public AjaxResult getDetailById(@PathVariable("sourceId") Long sourceId) {
        return AjaxResult.success(iSourceService.getSourceInfoById(sourceId));
    }

    /**
     * 根据身份证号查询患者信息
     */
    @PreAuthorize("@ss.hasPermi('biobank:source:query')")
    @GetMapping(value = "getPatientInfo/{idCard}")
    public AjaxResult getPatientInfoByIdCard(@PathVariable("idCard") String idCard) {
        return AjaxResult.success(iSourceService.getPatientInfoByIdCard(idCard));
    }

    /**
     * 根据身份证号查询患者用药信息
     */
    @PreAuthorize("@ss.hasPermi('biobank:source:query')")
    @GetMapping(value = "getPatientYYInfoByIdCard")
    public TableDataInfo getPatientYYInfoByIdCard(@Valid PatientTreatmentInfoDto dto) {
        //startPage();
        List<GetPatientYYInfoByIdCardVo> list = iSourceService.getPatientYYInfoByIdCard(dto);
        return NcPage(list);
    }


    /**
     * 导出患者用药信息
     */
    @PreAuthorize("@ss.hasPermi('biobank:source:export')")
    @Log(title = "导出患者用药信息 ", businessType = BusinessType.EXPORT)
    @PostMapping("/exportPatientYYInfo")
    public AjaxResult exportPatientYYInfo(@RequestBody ExportPatientYYInfoDto dto) {
        ExcelUtil<ExportYYInfoDto> util = new ExcelUtil<>(ExportYYInfoDto.class);
        return util.exportExcel(dto.getList(), "患者用药信息");
    }


    /**
     * 根据身份证号查询患者诊断信息
     */
    @PreAuthorize("@ss.hasPermi('biobank:source:query')")
    @GetMapping(value = "getPatientZDInfoByIdCard")
    public TableDataInfo getPatientZDInfoByIdCard(@Valid PatientTreatmentInfoDto dto) {
        //startPage();
        List<GetPatientZDInfoByIdCardVo> list = iSourceService.getPatientZDInfoByIdCard(dto);
        return NcPage(list);
    }

    /**
     * 内存分页
     *
     * @param list 列表
     * @return {@link TableDataInfo}
     */
    public TableDataInfo NcPage(List list) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        List<GetPatientZDInfoByIdCardVo> result = ListUtil.page(pageNum - 1, pageSize, list);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(result);

        if (list == null || list.isEmpty()) {
            rspData.setTotal(0);
        } else {
            rspData.setTotal(list.size());
        }

        return rspData;
    }

    /**
     * 导出患者诊断信息
     */
    @PreAuthorize("@ss.hasPermi('biobank:source:export')")
    @Log(title = "导出患者诊断信息 ", businessType = BusinessType.EXPORT)
    @PostMapping("/exportPatientZDInfo")
    public AjaxResult exportPatientZDInfo(@RequestBody ExportPatientZDInfoDto dto) {
        ExcelUtil<ExportZDInfoDto> util = new ExcelUtil<>(ExportZDInfoDto.class);
        return util.exportExcel(dto.getList(), "患者诊断信息");
    }


    /**
     * 根据就诊卡号查询患者治疗信息
     */
    @PreAuthorize("@ss.hasPermi('biobank:source:query')")
    @GetMapping(value = "getPatientTreatmentInfo")
    public TableDataInfo getPatientTreatmentInfo(@Valid PatientTreatmentInfoDto dto) {
        System.out.println("dto = " + dto.toString());
        //startPage();
        List<GetPatientTreatmentInfoVo> patientTreatmentInfos = iSourceService.getPatientTreatmentInfo(dto);
        return NcPage(patientTreatmentInfos);
    }


    /**
     * 导出患者治疗信息
     */
    @PreAuthorize("@ss.hasPermi('biobank:source:export')")
    @Log(title = "导出患者治疗信息 ", businessType = BusinessType.EXPORT)
    @PostMapping("/exportPatientTreatmentInfo")
    public AjaxResult exportPatientTreatmentInfo(@RequestBody ExportPatientTreatmentInfoDto dto) {
        ExcelUtil<ExportTreatmentInfoDto> util = new ExcelUtil<>(ExportTreatmentInfoDto.class);
        return util.exportExcel(dto.getList(), "患者治疗信息");
    }

    /**
     * 分页查询当天提醒患者就诊信息
     */
    @PreAuthorize("@ss.hasPermi('biobank:source:query')")
    @GetMapping(value = "getPatientRemindersInfoToday")
    public TableDataInfo getPatientRemindersInfoToday() {
        //startPage();
        List<GetPatientRemindersInfoTodayVo> patientTreatmentInfos = iSourceService.getPatientTreatmentInfo();

        return NcPage(patientTreatmentInfos);

    }

    /**
     * 查询当天提醒患者就诊信息的总数
     */
//    @PreAuthorize("@ss.hasPermi('biobank:source:remindTotal')")
    @PostMapping(value = "getPatientRemindersInfoTodayTotal")
    public AjaxResult getPatientRemindersInfoTodayTotal() {

        List<String> remindIdcardsToday = iSourceService.getRemindIdcardsToday();
        if (remindIdcardsToday == null || remindIdcardsToday.isEmpty()) {
            return AjaxResult.success(0);
        }
        return AjaxResult.success(remindIdcardsToday.size());
    }


    /**
     * 设置患者提醒
     */
    @PreAuthorize("@ss.hasPermi('biobank:source:reminder')")
    @Log(title = "样本源设置患者提醒 ", businessType = BusinessType.UPDATE)
    @PostMapping(value = "setUpPatientReminders")
    public AjaxResult setUpPatientReminders(@RequestBody SetUpPatientRemindersDto dto) {
        return toAjax(iSourceService.SetUpPatientReminders(dto));
    }


}
